package cn.dy.sbo.model;

import cn.common.lib.springside.entity.AutoIdEntity;
import cn.dy.sbo.contants.Constants;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import javax.persistence.*;

/**
 * 
 * 用户
 * 
 * @author zzHe
 * @version 1.0
 * @since 2013-2-22
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name = "tuser")
public class User extends AutoIdEntity
{
    private static final long serialVersionUID = 1L;
    
    private String            username;               // 用户名

    private String            idNumber;               // 身份证

    private String            mobile;                 // 手机

    private String            name;                   // 姓名
    
    private String 			  enname;	//en名称

    private String            password;               // 密码

    private String			  role; 

    private String            telephone;              // 固话

    
    private String            company;                // 单位
    
    private String			  contact;				//联系人
    
    private String			  address;				//地址

    private boolean           valid            = true; // 是否可登录
    
    private String				code;
    
    private Partner			partner;

    
    @Column(length=20)
	public String getUsername() {
		return username;
	}



	public void setUsername(String username) {
		this.username = username;
	}


	@Column(length=20)
	public String getIdNumber() {
		return idNumber;
	}



	public void setIdNumber(String idNumber) {
		this.idNumber = idNumber;
	}


	@Column(length=20)
	public String getMobile() {
		return mobile;
	}



	public void setMobile(String mobile) {
		this.mobile = mobile;
	}


	@Column(length=200)
	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}
	
	@Column(length=200)
	public String getEnname() {
		return enname;
	}



	public void setEnname(String enname) {
		this.enname = enname;
	}



	@Column(length=40)
	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}


	@Column(length=40)
	public String getRole() {
		return role;
	}



	public void setRole(String role) {
		this.role = role;
	}


	@Column(length=20)
	public String getTelephone() {
		return telephone;
	}



	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}


	@Column(length=100)
	public String getCompany() {
		return company;
	}



	public void setCompany(String company) {
		this.company = company;
	}


	@Column(length=20)
	public String getContact() {
		return contact;
	}


	public void setContact(String contact) {
		this.contact = contact;
	}


	@Column(length=200)
	public String getAddress() {
		return address;
	}



	public void setAddress(String address) {
		this.address = address;
	}



	public boolean isValid() {
		return valid;
	}



	public void setValid(boolean valid) {
		this.valid = valid;
	}

	
	@Column(length=20)
	public String getCode() {
		return code;
	}



	public void setCode(String code) {
		this.code = code;
	}

	
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
	public Partner getPartner() {
		return partner;
	}

	public void setPartner(Partner partner) {
		this.partner = partner;
	}

    @Transient
    public String getRoleStr(){
        if(role == null)
            return null;
        String[] roles = role.split(",") ;
        StringBuffer sb = new StringBuffer("1");
        for(String r:roles)  {
              sb.append(",") ;
              sb.append(Constants.ROLE_MAP.get(r)) ;
        }
        return sb.toString().replace("1,","") ;
    }

	@Override
    public String toString()
    {
        return ToStringBuilder.reflectionToString(this);
    }

}
